package com.wthink.newMain.stationStudy.viewmodel


import android.util.Log
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.wthink.hzgj.BusApplication
import com.wthink.hzgj.data.BusRepository
import com.wthink.hzgj.data.Result
import com.wthink.hzgj.data.repository.DefaultBusRepository
import com.wthink.hzgj.home.data.Line
import com.wthink.hzgj.service.DevService
import com.wthink.hzgj.station.data.DefaultStationRepository
import com.wthink.hzgj.station.data.Station
import com.wthink.hzgj.util.Page
import com.wthink.hzgj.util.toast
import com.wthink.newMain.data.LoadStationData
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class StationStudyViewmodel : ViewModel() {

    var listLine = MutableLiveData(emptyList<String>())
    var arr = ArrayList<String>()
    var isUir = MutableLiveData<Boolean>()
    var stationList = MutableLiveData(emptyList<Station>())
    val lineId = MutableLiveData<String>()
    val current = MutableLiveData<String>()

    var loadStationData = MutableLiveData<LoadStationData>()

    private val busRepository: BusRepository = DefaultBusRepository()

    private val repository = DefaultStationRepository()


    val lineList = MutableLiveData(
        Page(
            emptyList<Line>(), 0, 1
        )
    )

    fun start() {
        viewModelScope.launch(Dispatchers.IO) {
            if (lineList.value!!.list.isEmpty()) {
                val linePage = busRepository.getLinePage(lineList.value!!.page, true)
                lineList.postValue(linePage)
                for (line in linePage.list) {
                    arr.add(line.name)
                }
                listLine.postValue(arr)
            }
        }
    }

    fun updateStationList(lineId: String, isUir: Boolean) {
        viewModelScope.launch(Dispatchers.IO) {
            stationList.postValue(repository.getStationList(lineId, isUir))
        }
    }

    fun findLineName(lineName: String, isUir: Boolean) {
        viewModelScope.launch(Dispatchers.IO) {
            busRepository.getLinePage(lineList.value!!.page, true).apply {
                if (list.isNotEmpty()) {
                    for (line in list) {
                        if (lineName == line.name) {
                            updateStationList(line.id, isUir)
                            lineId.postValue(line.id)
                            return@apply
                        }
                    }
                }
            }
        }
    }

    fun select(station: Station) {
        val newStationList = stationList.value!!.map {
            Station(it.id, it.name, it.innerId, it.id.equals(station.id))
        }

        if (newStationList.isNotEmpty()) {
            newStationList.last().end = true
        }

        stationList.postValue(newStationList)
        current.postValue(station.name)
        viewModelScope.launch(Dispatchers.IO) {
            val ret = DevService.getStationData(lineId.value!!, station.id)
            if (ret is Result.Success) {
                loadStationData.postValue(ret.data)
            }
        }
    }

    fun importStation() {
        viewModelScope.launch(Dispatchers.IO) {
            val ret = DevService.importStationData()
            if (ret is Result.Success) {
                withContext(Dispatchers.Main) {
                    toast(BusApplication.busContext, "导入请求成功")
                }

            }
        }

    }

    fun exportStation() {
        viewModelScope.launch(Dispatchers.IO) {
            val ret = DevService.exportStationData()
            if (ret is Result.Success) {
                withContext(Dispatchers.Main) {
                    toast(BusApplication.busContext, "导出请求成功")
                }
            }
        }

    }

    fun loadStation(station: Station) {
        viewModelScope.launch(Dispatchers.IO) {
            val ret = DevService.loadStationData(lineId.value!!, station.id)
            if (ret is Result.Success) {
                withContext(Dispatchers.Main) {
                    toast(BusApplication.busContext, "站点录入成功")
                }
            }
        }

    }
}